﻿package lib 
{
	import com.greensock.TweenLite;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	
	/**
	 * ...
	 * @author kabuto.boy
	 */
	public class Application extends Sprite
	{
		public static const APP_VERSION:String = "0.1";
		
		private var soundLoader:SoundLoader;
		
		private var type:String = "stop";
		private var playing:Boolean = false;
		
		private var stw:int;
		private var sth:int;
		
		private var hasSound:Boolean = false;
		private var changed:Boolean = false;
		
		public function Application() 
		{
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.showDefaultContextMenu = false;
			
			stw = 250;
			sth = 30;
			
			soundLoader = new SoundLoader();
			
			action_btn.addEventListener(MouseEvent.CLICK, onEvent);
			volume_btn.addEventListener(MouseEvent.CLICK, onEvent);
			volumeScrubbee.scrubber.addEventListener("scrub", onEvent);
			volumeScrubbee.scrubber.addEventListener("scrubbing", onEvent);
			volumeScrubbee.scrubber.addEventListener("scrubed", onEvent);
			volumeScrubbee.scrubbar.addEventListener("scrubed", onEvent);
			playScrubbee.scrubber.addEventListener("scrub", onEvent);
			playScrubbee.scrubber.addEventListener("scrubbing", onEvent);
			playScrubbee.scrubber.addEventListener("scrubed", onEvent);
			playScrubbee.scrubbar.addEventListener("scrubed", onEvent);
			sourcee.source_txt.addEventListener(Event.CHANGE, onEvent);
			
			setVolume(1);
		}
		
		private function onEvent(event:*):void
		{
			var _target:Object = event.target; 
			
			switch(event.type) {
				case "click" :
					switch(_target.parent.name) {
						case "action_btn" :
							switch(type) {
								case "stop" :
									if (changed) {
										soundLoader.load(sourcee.source_txt.text);
										
										soundLoader.addEventListener(Event.COMPLETE, onEvent);
										soundLoader.addEventListener(IOErrorEvent.IO_ERROR, onEvent);
										soundLoader.addEventListener(ProgressEvent.PROGRESS, onEvent);
										
										soundLoader.addEventListener("check", onEvent);
										soundLoader.addEventListener(Event.SOUND_COMPLETE, onEvent);
										
										sourcee.filledbar.scaleX = 0;
										changed = false;
									} else 
										play(true);
									break;
								case "play" :
									stop();
									break;
								case "volume" :
									if (hasSound) {
										if (!soundLoader.soundCompleted) {
											if(soundLoader.playing) {
												moveTo("play");
												action_btn.symbol.gotoAndStop(2);
												type = "play";
											} else {
												moveTo("stop");
												action_btn.symbol.gotoAndStop(1);
												type = "stop";
											}
										} else {
											moveTo("stop");
											type = "stop";
										}
									} else 
										moveTo("stop");
									break;
							}
							break;
						case "volume_btn" :
							volumeClick();
							break;
					}
					break;
				case "complete" :
					hasSound = true;
					play();
					break;
				case "ioError" :
					
					break;
				case "progress" :
					sourcee.filledbar.scaleX = soundLoader.loaded;
					break;
				case "check" :
					playScrubbee.scrubber.scrubTo(soundLoader.position);
					break;
				case "scrub" :
					switch(_target.parent.name) {
						case "volumeScrubbee" :
							
							break;
						case "playScrubbee" :
							soundLoader.pause();
							break;
					}
					break;
				case "scrubbing" :
					switch(_target.parent.name) {
						case "volumeScrubbee" :
							setVolume(_target.position);
							break;
						case "playScrubbee" :
							
							break;
					}
					break;
				case "scrubed" :
					switch(_target.parent.name) {
						case "volumeScrubbee" :
							
							break;
						case "playScrubbee" :
							soundLoader._play(_target.position);
							break;
					}
					break;
				case "change" :
					changed = true;
					break;
				case "soundComplete" :
					stop(true);
					break;
			}
		}
		
		private function play(resume:Boolean = false):void
		{
			if (hasSound) {
				action_btn.symbol.gotoAndStop(2);
				
				moveTo("play");
				
				if (!resume)
					soundLoader.play();
				else
					soundLoader.resume();
				
				type = "play";
			}
		}
		
		private function moveTo(to:String):void
		{
			switch(to) {
				case "play" :
					TweenLite.to(sourcee, 0.3, { x: stw } );
					TweenLite.to(volume_btn, 0.3, { x: 0 } );
					TweenLite.to(action_btn, 0.3, { x: stw - 30 } );
					TweenLite.to(playScrubbee, 0.3, { x: 0 + 30 } );
					TweenLite.to(volumeScrubbee, 0.3, { x: 0 - 190 } );
					break;
				case "volume" :
					TweenLite.to(playScrubbee, 0.3, { x: 0 + 30 } );
					TweenLite.to(volumeScrubbee, 0.3, { x: 0 } );
					TweenLite.to(volume_btn, 0.3, { x: 190 } );
					TweenLite.to(action_btn, 0.3, { x: 190 + 30 } );
					TweenLite.to(sourcee, 0.3, { x: 190 + 30 + 30 } );
					break;
				case "stop" :
					TweenLite.to(sourcee, 0.3, { x: 0 + 30 + 30 } );
					TweenLite.to(volume_btn, 0.3, { x: 0 } );
					TweenLite.to(action_btn, 0.3, { x: 0 + 30 } );
					TweenLite.to(playScrubbee, 0.3, { x: 0 - 190 + 30 } );
					TweenLite.to(volumeScrubbee, 0.3, { x: 0 - 190 } );
					break;
			}
		}
		
		private function stop(end:Boolean = false):void
		{
			action_btn.symbol.gotoAndStop(1);
			
			moveTo("stop");
			
			if (hasSound) {
				if(end) {
					soundLoader.stop();
				} else {
					soundLoader.pause();
				}
			}
			
			type = "stop";
		}
		
		private function volumeClick():void
		{
			action_btn.symbol.gotoAndStop(1);
			
			moveTo("volume");
			
			type = "volume";
		}
		
		private function setVolume(value:Number):void
		{
			soundLoader.volume = value;
			
			if(value > 0.00001) 
				volume_btn.symbol.gotoAndStop(Math.ceil(value * 2) + 1);
			else
				volume_btn.symbol.gotoAndStop(1);
			
			volumeScrubbee.scrubber.scrubTo(value);
		}
		
	}

}